package org.zattix.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.zattix.bean.ZLog;
import org.zattix.ejb.ZLogFacade;

/**
 *
 * @author s.rainoldi
 */
public class Logger {
    
    public final static ZLogFacade zLogFacade = lookupZLogFacadeBean();
    
    public static int INFO    = 0;
    public static int WARNING = 1;
    public static int ERROR   = 2;
    
    public static void log(String text, int type){
        Calendar date = Calendar.getInstance();
         SimpleDateFormat dateformatter = new SimpleDateFormat("dd.mm.yyyy hh:mm:ss");
        String stringDate = dateformatter.format(date.getTime());
        
        ZLog zlog = new ZLog();
        zlog.setDate(date);
        zlog.setText(text);
        
        if(type == Logger.INFO){
            System.out.println("INFO . " + stringDate + " - " + text);
            zlog.setType("INFO");
        }
        else if (type == Logger.WARNING){
            System.out.println("WARNING . " + stringDate + " - " + text);
            zlog.setType("WARNING");
        }
        else if(type == Logger.ERROR){
            System.out.println("ERROR . " + stringDate + " - " + text);
            zlog.setType("ERROR");
        }
        Logger.zLogFacade.create(zlog);
    }
    public static void log(String text){
        log(text, Logger.INFO);
    }

    public static ZLogFacade lookupZLogFacadeBean() {
        try {
            Context c = new InitialContext();
            return (ZLogFacade) c.lookup("java:global/zattix/ZLogFacade!org.zattix.ejb.ZLogFacade");
        } catch (NamingException ne) {
            throw new RuntimeException(ne);
        }
    }

    
    
    
    
}
